

import { defineStore } from 'pinia'


import { requestCategory1, requestCategory2, requestCategory3 } from '@/api/attr'
import type { reqCategoryType } from '@/api/attr'
import { ElMessage } from 'element-plus';

//state类型
interface attrStoreType {
  categoryList1: reqCategoryType;
  categoryList2: reqCategoryType;
  categoryList3: reqCategoryType;
  category1Id: number | null;
  category2Id: number | null;
  category3Id: number | null;
}

const useattrStore = defineStore("attr", {
  state: (): attrStoreType => {
    return {
      categoryList1: [],
      categoryList2: [],
      categoryList3: [],
      category1Id: null,
      category2Id: null,
      category3Id: null,
    }
  },
  getters: {

  },
  actions: {
    //一级请求函数
    async getCategory1() {
      try {
        const result = await requestCategory1()
        //把一级列表的数据设置给state
        this.categoryList1 = result
      } catch (e) {
        ElMessage.error("获取一级shubai")
      }
    },
    //二级
    async getCategory2() {
      try {
        const result = await requestCategory2(this.category1Id as number)
        //把2级列表的数据设置给state
        this.categoryList2 = result
      } catch (e) {
        ElMessage.error("获取2级shubai")
      }
    },
    //3级
    async getCategory3() {
      try {
        const result = await requestCategory3(this.category2Id as number)
        //把一级列表的数据设置给state
        this.categoryList3 = result
      } catch (e) {
        ElMessage.error("获取3级shubai")
      }
    }
  }
})

export default useattrStore
